<%@ page import="com.dsj.dao.CustomerDao" %>
<%@ page import="com.dsj.entity.CustomerEntity" %>
<%@ page import="com.dsj.util.Encrypt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户登录结果</title>
</head>
<body>
<div style="margin: 0 auto">
    <%
        //实例化CustomerDao对象
        CustomerDao customerDao = new CustomerDao();

        //设置请求字符编码为utf-8
        request.setCharacterEncoding("utf-8");

        //获取名为name的请求参数
        String name = request.getParameter("name");

        // 从请求中获取名为"password"的参数值，并赋给变量password
        String password = request.getParameter("password");
        // 加密
        password = Encrypt.encryptPassword(password);

        //调用CustomerDao对象的selectCustomerEntity方法，根据name查询并返回CustomerEntity对象
        CustomerEntity customerEntity = customerDao.selectCustomerEntity(name);

        if(customerEntity == null) {  // 如果customerEntity为空
            //设置提示信息，提示用户名不存在
            String message = "用户名不存在！请重新登录";
            request.getSession().setAttribute("message", message);

            //跳转到登录页
            response.sendRedirect(request.getContextPath() + "/pages/product/main.jsp");
        } else {
            //存在此用户，将数据库中的密码取出来和用户输入的密码进行对比
            if(customerEntity.getPassword().equals(password)) {
                //设置提示信息，提示登录成功
                String message = "登录成功";
                request.getSession().setAttribute("message", message);

                //密码匹配成功，将用户信息存入session中
                session.setAttribute("user", customerEntity);
                session.setAttribute("name", name);
                //跳转到首页
                response.sendRedirect(request.getContextPath() + "/pages/product/main.jsp");
            } else {
                //设置提示信息，提示密码错误
                String message = "密码错误！，请重新登录";
                request.getSession().setAttribute("message", message);

                //密码匹配失败，跳转到登录页面
               response.sendRedirect(request.getContextPath() + "/pages/product/main.jsp");
            }
        }
    %>
</div>
</body>
</html>
